﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraBars;
using System.IO.Compression;
using POS.Framework;
using BLL;
using Model.Bill;
using Model;
using DevExpress.Xpo;
using Common.user32;
using Cofco.login;
using POS.ModForm;
using DevExpress.XtraBars.Alerter;

namespace POS
{
    public partial class frmMain : DevExpress.XtraBars.Ribbon.RibbonForm
    {
        Login frmLogin;
        public bool IsLockScreen = false;//是否锁定屏幕
        public frmMain(Login frmlogin)
        {
            InitializeComponent();
            this.frmLogin = frmlogin;
        }
        private void frmMain_Load(object sender, EventArgs e)
        {
            this._barUserInfo.Caption = string.Format(this._barUserInfo.Caption, MemberInfo.Store.StoreName, MemberInfo.User.UserName);
        }
        private void frmMain_Shown(object sender, EventArgs e)
        {
            if (ProductBLL.ProductCount() == 0)
            {
                frmDownloadData dlform = new frmDownloadData();
                dlform.ShowDialog();
            }
        }
        void Icon_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string childString = string.Empty;
            if (!string.IsNullOrEmpty(e.Item.Description))
                FormFactory.OpenForm(e.Item.AccessibleName, e.Item.Caption, e.Item.Description, this);
            else
                FormFactory.OpenForm(e.Item.AccessibleName, e.Item.Caption, this);
        }
        private void frmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            this.Dispose();
            Environment.Exit(0);
            System.Diagnostics.Process.GetCurrentProcess().Kill();
        }

        private void timerUnitWork_Tick(object sender, EventArgs e)
        {
            long i = MouseKey.getIdleTick();
            if (i > 900000 && IsLockScreen == false)//(i > 10 * 1000)//超过一分钟
            {
                frmLockScreen lockScreen = new frmLockScreen(this);
                lockScreen.ShowDialog();
            }
            MonitorHeartBeat();
        }

        #region 客户端心跳监控器

        UserBLL userBll = new UserBLL();
        int HeartBeatCount = 0;
        void MonitorHeartBeat() 
        {
            if (HeartBeatCount % 40 != 0) return;
            HeartBeatCount++;

            string msg=string.Empty;
            if (userBll.HeartBeat(out msg))
            {
                this._barServerState.Caption = "服务器状态：已连接";
                this._barServerState.Glyph = global::POS.Properties.Resources.show_16x16;
            }
            else 
            {
                this._barServerState.Caption = "服务器状态：未连接";
                this._barServerState.Glyph = global::POS.Properties.Resources.hide_16x16;
            }
            if (!string.IsNullOrEmpty(msg)) 
            {
                MsgBox.ShowWarning(msg);
            }
        }
        #endregion

        #region 更换当前店柜信息
        private void _barUserInfo_ItemClick(object sender, ItemClickEventArgs e)
        {
            frmStoreChange storeChange = new frmStoreChange();
            if (storeChange.ShowDialog() == System.Windows.Forms.DialogResult.OK) 
            {
                this._barUserInfo.Caption = string.Format("登录门店：{0}  登录帐号：{1}", MemberInfo.Store.StoreName, MemberInfo.User.UserName);
            }
        }
        #endregion

        public void ShowMsg(AlertInfo info) 
        {
            this.alertControl.Show(this, info);
        }
    }
}